﻿'Imports System.Data.SqlClient
Imports System.IO

Module ModuloValidaciones
    Public Sub Number(ByVal a As System.Windows.Forms.KeyPressEventArgs)
        Dim vKeyascii As Integer
        vKeyascii = Microsoft.VisualBasic.Asc(a.KeyChar)
        If vKeyascii <> 8 Then
            If vKeyascii < 48 Or vKeyascii > 57 Then
                a.Handled = True
            End If
        End If
    End Sub
    Public Sub SoloLetras(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Char.IsLetter(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsControl(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub
    Public Sub SoloNumeros(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Char.IsDigit(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsControl(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub
    Public Sub SoloAlfanumerico(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Char.IsLetterOrDigit(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsControl(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub
    Public Function ExistFile(ByVal _strPathFile As String) As Boolean
        Try
            If File.Exists(_strPathFile) Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return ExistFile = False
        End Try
    End Function
    Public Function ExistDirectory(ByVal _strPathDirectory As String) As Boolean
        Try
            If Directory.Exists(_strPathDirectory) Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return ExistDirectory = False
        End Try
    End Function

    Public Function ValidEmailAddress(ByVal emailAddress As String, ByRef errorMessage As String) As Boolean
        If emailAddress.Length = 0 Then
            errorMessage = "e-mail address is required."
            Return False

        End If

        Dim expression As String = "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"

        Dim regex As New System.Text.RegularExpressions.Regex(expression)
        If regex.IsMatch(emailAddress) Then
            errorMessage = ""
            Return True
        End If

        errorMessage = "La dirección de correo electrónico debe estar en un formato válido." & vbNewLine _
        & "Por Ejemplo : 'usuario@ejemplo.com' "
        Return False

    End Function

#Region " Método que habilita/deshabilita los textbox del formulario frmSetAccount "

    Public Sub EnableTextBox(ByVal flag As Boolean)
        frmSetAccount.txtUserAccount.Enabled = flag
        frmSetAccount.txtAccountPass.Enabled = flag
        frmSetAccount.txtAccountConfirPass.Enabled = flag
    End Sub

    Public Sub SetAccountFolder()
        g_StrAccountUserFolder = frmSetAccount.txtUserAccount.Text
        g_StrPasswordUserFolder = frmSetAccount.txtAccountPass.Text
    End Sub

    Public Function validationAccountFolder() As Boolean
        Dim result As Boolean = False

        If frmSetAccount.txtUserAccount.Text.Length > 0 Then
            result = True
        End If
        If frmSetAccount.txtAccountPass.Text.Length > 0 And frmSetAccount.txtAccountConfirPass.Text.Length > 0 Then
            If frmSetAccount.txtAccountPass.Text = frmSetAccount.txtAccountConfirPass.Text Then
                result = True
            Else
                result = False
            End If
        Else
            result = False
        End If


        Return result
    End Function


    Public Sub MessageMistake(ByVal p_Message As String, Optional ByVal p_TypeMessage As Boolean = Nothing)
        If p_TypeMessage Then
            MsgBox(p_Message, MsgBoxStyle.Information, g_ApplicationName)
            Exit Sub
        End If

        MsgBox(p_Message, MsgBoxStyle.Critical, g_ApplicationName)

    End Sub


    Public Function ValidaCaracteresInvalidos(ByVal p_KeyAscii As Integer, ByVal p_ExString As String, ByVal p_Bool As Integer, ByVal p_Return As Integer)
        Dim Var_TmpAscii As Integer
        Dim Var_ReturnAscii As Integer
        If p_KeyAscii = 8 Then
            ValidaCaracteresInvalidos = False
            Exit Function
        End If
        If p_KeyAscii = 13 Then
            Var_ReturnAscii = True
        Else
            Var_ReturnAscii = False
        End If
        If p_ExString <> "" Then
            If p_Bool = False Then
                If (InStr(p_ExString, Chr(p_KeyAscii)) <> 0) Then
                    Var_TmpAscii = True
                End If
            ElseIf p_Bool = True Then
                If (InStr(p_ExString, Chr(p_KeyAscii)) = 0) And Var_TmpAscii <> 13 Then
                    Var_TmpAscii = True
                End If
            End If
        End If
        ValidaCaracteresInvalidos = Var_TmpAscii
        If p_Return = True And Var_ReturnAscii = True Then
            SendKeys.Send("{TAB}")
        End If
    End Function

#End Region
End Module
